package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;

    //异步线程优化
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItemInfo(Long albumId) {
        //判断专辑id是否存在与布隆过滤器中 不存在直接不让看
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean contains = bloomFilter.contains(albumId);
        if(!contains){
            throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }

        //多线程中 不能使用hashMap 应该使用线程安全的ConcurrentHashMap
       Map<String, Object> mapResult = new ConcurrentHashMap<>();
        //专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {

            AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfoById(albumId).getData();
            Assert.notNull(albumInfo, "专辑信息为空");
            mapResult.put("albumInfo", albumInfo);
            return albumInfo;

        }, threadPoolExecutor);
        //专辑统计信息

        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {

            AlbumStatVo albumStatVo = albumInfoFeignClient.getAlbumStat(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计信息为空");
            mapResult.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor);

        //专辑分类
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {

            BaseCategoryView baseCategoryView = albumInfoFeignClient.getBaseCategoryViewByCategory3Id(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "分类信息为空");
            mapResult.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);
        //主播信息

        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {

            UserInfoVo userInfoVo = userFeignClient.getUserInfoVoByUserId(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "主播信息为空");
            mapResult.put("announcer", userInfoVo);
        }, threadPoolExecutor);


        //全部执行完 ->往下走
        CompletableFuture.allOf(albumInfoCompletableFuture,
                statCompletableFuture,
                userCompletableFuture,
                baseCategoryViewCompletableFuture).join();

        return mapResult;
    }
}
